package edu.stanford.bmir.protege.web.client.ui.ontology.transform;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;

import edu.stanford.bmir.protege.web.client.dispatch.DispatchServiceManager;
import edu.stanford.bmir.protege.web.client.rpc.data.DocumentId;
import edu.stanford.bmir.protege.web.client.ui.library.msgbox.MessageBox;
import edu.stanford.bmir.protege.web.client.ui.library.progress.ProgressMonitor;
import edu.stanford.bmir.protege.web.client.ui.ontology.transform.service.TransformService;
import edu.stanford.bmir.protege.web.client.ui.ontology.transform.service.TransformServiceAsync;
import edu.stanford.bmir.protege.web.client.ui.util.UIUtil;
import edu.stanford.bmir.protege.web.server.transform.service.StringWord;
import edu.stanford.bmir.protege.web.shared.merge.MergeUploadedProjectAction;
import edu.stanford.bmir.protege.web.shared.merge.MergeUploadedProjectResult;
import edu.stanford.bmir.protege.web.shared.project.ProjectId;
import edu.stanford.bmir.protege.web.shared.transform.MappingBean;


public class MainView extends Composite {
	
	private final TransformServiceAsync service = GWT.create(TransformService.class);
	
	private VerticalPanel vPanel = new VerticalPanel();
	private VerticalPanel contentPanel;
	
	private List<String> sels = new ArrayList<String>();
	
	private Map<String,String> conts = new HashMap<String,String>();
	
	private List<MappingBean> mblst = new ArrayList<MappingBean>();
	
	private String fileName;
	
	private Map<String,String> mapping;
	
	StepTwo two;
	
	ProjectId projectId;
	
	
	public MainView ( ProjectId projectId ) {
		
		initWidget( this.vPanel );
		
		this.projectId = projectId;
		
		MenuView menu = new MenuView(this);
		this.vPanel.add( menu );
		
		this.contentPanel = new VerticalPanel();
		this.vPanel.add(contentPanel);
		contentPanel.setBorderWidth(0);
		
		StepOne one = new StepOne( this );
		this.contentPanel .add(one); 
		
	}
	
	public ProjectId getProjectId () {
		return projectId;
	}
	
	public void openStepOne() {
		
		sels = new ArrayList<String>();
		conts = new HashMap<String,String>();
		
		fileName = null;
		
		this.contentPanel.clear();
		StepOne one = new StepOne( this );
		this.contentPanel.add( one );
		
	}
	
	public void openStepTwo() {
		
		if ( fileName == null || fileName == "" ) {
			MessageBox.showMessage("请先上传导入文件！");  
		} else {
			
			getMblst().clear();
			getConts().clear();
			getSels().clear();
			
			this.contentPanel.clear();  
			two = new StepTwo( this );
			this.contentPanel.add( two );
		}
		
	}
	public void openStepThree() {
		
		if ( sels.isEmpty() ) {
			MessageBox.showMessage("请选择导入的映射关系");  
		} else {
			this.contentPanel.clear();
			
			StepThree three = new StepThree( this );
			this.contentPanel.add( three );
			
		}
	}
	/**
	 * 生成owl文件
	 */
	public void createOWL () {
		
		Map<String,String> pros = new HashMap<String,String>();
		
		UIUtil.showLoadProgessBar("分析", "分析上传文件中");
		
		for ( MappingBean mb : mblst ) {
			String name = mb.getName();
			//映射USE字段和USE+字段
			if ( !StringWord.USE.equals( name ) && !StringWord.USEO.equals( name )  ) {
				pros.put( mb.getName() , mb.getMapping());
			}
		}
		
		service.createOWL(fileName, getProjectId(),pros,new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable caught) {
				
			}

			@Override
			public void onSuccess(String result) {
				
				UIUtil.hideLoadProgessBar();
				
				performMerge(getProjectId(), new DocumentId( result ),"导入");
				
				openStepOne();
				
			}
			
		});
		
	}
	
	 
	 private void performMerge(ProjectId projectId, DocumentId uploadedProjectDocumentId, String commitMessage) {
		 DispatchServiceManager dispatchServiceManager = DispatchServiceManager.get();
		 ProgressMonitor.get().showProgressMonitor("正在导入中...", "请稍后...");
		 dispatchServiceManager.execute(new MergeUploadedProjectAction(projectId, uploadedProjectDocumentId, commitMessage), new AsyncCallback<MergeUploadedProjectResult>() {

				@Override
				public void onFailure(Throwable arg0) {
					ProgressMonitor.get().hideProgressMonitor();
				}

				@Override
				public void onSuccess(MergeUploadedProjectResult arg0) {
					ProgressMonitor.get().hideProgressMonitor();
					MessageBox.showMessage("项目已成功导入");
				}
	        });
	}
	
	public int getSelectCount() {
		
		int length = two.getSelectCount();
		
		return length;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public Map<String, String> getMapping() {
		return mapping;
	}

	public void setMapping(Map<String, String> mapping) {
		this.mapping = mapping;
	}

	public List<String> getSels() {
		return sels;
	}

	public void setSels(List<String> sels) {
		this.sels = sels;
	}

	public Map<String, String> getConts() {
		return conts;
	}

	public void setConts(Map<String, String> conts) {
		this.conts = conts;
	}

	public List<MappingBean> getMblst() {
		return mblst;
	}

	public void setMblst(List<MappingBean> mblst) {
		this.mblst = mblst;
	}
	
}
